<?php
/**
 * flile:RpcClientOne.php
 * author:mirgong
 * create_time:2021/10/21 10:14
 * e_mail:451716286@qq.com
 */

namespace App\HttpController;

use EasySwoole\Component\Di;
use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Http\Message\Status;
use EasySwoole\Rpc\Protocol\Response;
use EasySwoole\Rpc\Rpc;

class RpcClientOne extends Controller
{
    public function getRpcObjWithDi()
    {
        return Di::getInstance()->get("rpc_memory_object");
    }

    public function callRpcServerWithDi()
    {
        $rpc = $this->getRpcObjWithDi();

        $ret = [];
        $client = $rpc->client();
        $client->setClientArg([1,2,3]);
        /**
         * 调用商品列表
         */
        $ctxl = $client->addRequest("Goods.GoodsModule.list");
        // 设置请求参数
        $ctxl->setArg(["a","b","c"]);
        // 设置调用成功执行回调
        $ctxl->setOnSuccess(function (Response $response)use (&$ret){
            $ret[] = [
              "list"=>[
                  "msg"=>$response->getMsg(),
                  "result"=>$response->getResult()
              ]
            ];
        });

        /**
         * 调用信箱公共
         */
        $ctx2 = $client->addRequest("Common.CommonModule.mailBox");
        $ctx2->setOnSuccess(function (Response  $response)use (&$ret){
           $ret[] = [
             "mailBox"=>[
                   "msg" =>$response->getMsg(),
                  "result"=>$response->getResult()
                ]
           ];
        });

        /**
         * 获取系统时间
         */
        $ctx2 = $client->addRequest("Common.CommonModule.serverTime");
        $ctx2->setOnSuccess(function (Response $response)use (&$ret){
            $ret[] = [
                "serverTime"=>[
                    "msg"=>$response->getMsg(),
                    "result"=>$response->getResult()
                ]
            ];
        });
        $client->exec();
        $this->writeJson(Status::CODE_OK,$ret);
    }
}